package java6699.dao.gui;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import javax.swing.*;
import java.net.URL;
import java.util.List;
import java.util.Optional;

public class EditStudent6699Controller {

    @FXML
    private BorderPane root;

    @FXML
    private TableView<Student6699> tvStudents;

    @FXML
    private TableColumn<Student6699, String> colSno;

    @FXML
    private TableColumn<Student6699, String> colSname;

    @FXML
    private TableColumn<Student6699, Integer> colLx1;

    @FXML
    private TableColumn<Student6699, Integer> colLx2;

    private ObservableList<Student6699> oList;

    private Stage dialog;
    private Student6699Dao dao=new Student6699Dao();
    private Dialog6699Controller controller;
    private Alert alert;
    private List<Student6699> stuList;

    @FXML
    void initialize() {
        //设置TableView中各列对应显示Student6699的属性
        colSno.setCellValueFactory(new PropertyValueFactory<>("sno"));
        colSname.setCellValueFactory(new PropertyValueFactory<>("sname"));
        colLx1.setCellValueFactory(new PropertyValueFactory<>("lx1"));
        colLx2.setCellValueFactory(new PropertyValueFactory<>("lx2"));

        //调用Student6699Dao的findAll6699方法将xslist表中的数据加载到集合，作为TableView的数据源
        stuList =dao.findAll6699();
        tvStudents.getItems().addAll(stuList);
        alert =new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("数据维护窗口");
    }
    private  void alterMessage(String header,String msg){
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("学生信息编辑窗口");
        alert.setHeaderText(header);
        alert.setContentText(msg);
        alert.show();

    }
    private void createDialog6699(){
        URL url=getClass().getResource("Dialog6699View.fxml");
        FXMLLoader loader=new FXMLLoader(url);
        Parent dialogRoot=null;
        try {
            dialogRoot=loader.load();
            controller=loader.getController();
        }catch (Exception e){
            e.printStackTrace();
        }
        Scene scene=new Scene(dialogRoot);
        dialog =new Stage();
        dialog.setScene(scene);
        Stage main=(Stage) root.getScene().getWindow();
        dialog.initModality(Modality.WINDOW_MODAL);
        dialog.setResizable(false);
        dialog.setTitle("学生编辑窗口");
    }

    @FXML
    void add(ActionEvent event) {
        if (dialog ==null){
            createDialog6699();
        }
        controller.initAdd6699();
        dialog.showAndWait();
        Student6699 stu=controller.getStudent();
        if (stu !=null){
            int id=dao.add6699(stu);
            if (id>0){
                tvStudents.getItems().add(stu);
                tvStudents.refresh();
                tvStudents.requestFocus();
                tvStudents.getSelectionModel().select(stu);
                tvStudents.scrollTo(stu);

            }else {
                alterMessage("数据库错误","("+stu+")无法添加数据!");
            }
        }
    }
    private Student6699 getCurrentStudent(){
        int index=tvStudents.getSelectionModel().getSelectedIndex();
        if (index<0){
            alterMessage("请注意","请选择需要修改的数据!");
            return null;
        }
        return tvStudents.getItems().get(index);
    }

    @FXML
    void change(ActionEvent event) {
        Student6699 oldStudent=getCurrentStudent();
        if (oldStudent ==null) return;
        if (dialog ==null){
            createDialog6699();
        }
        controller.initChange6699(oldStudent);
        dialog.showAndWait();
        Student6699 newStudent=controller.getStudent();
        if (newStudent ==null||newStudent.getId()==oldStudent.getId()){

        }else {
            int result =dao.update6699(oldStudent.getId(),newStudent);
            if (result ==1){
                newStudent.setId(oldStudent.getId());
                int index=tvStudents.getSelectionModel().getSelectedIndex();
                tvStudents.getItems().set(index,newStudent);
                tvStudents.refresh();
                tvStudents.getSelectionModel().select(newStudent);
                tvStudents.requestFocus();
            }else {
                alterMessage("数据库错误","无法更新("+newStudent+")!");
            }
        }
    }

    @FXML
    void delete(ActionEvent event) {
        Student6699 stu=getCurrentStudent();
        if (stu==null) return;
        alert.setHeaderText("请选择");
        alert.setContentText("请确定是否删除？"+stu.getSno());
        alert.getButtonTypes().setAll(ButtonType.YES,ButtonType.NO);
        Optional<ButtonType> type=alert.showAndWait();
        type.ifPresent(buttonType -> {
            if (buttonType ==ButtonType.YES){
                int i=dao.delete6699(stu.getId());
                if (i==1){
                    tvStudents.getItems().remove(stu);
                    tvStudents.refresh();
                    tvStudents.requestFocus();
                }else {
                    alterMessage("数据库错误","无法删除("+stu+")!");
                }
            }
        });
    }

    @FXML
    void doubleClick(MouseEvent event) {
        if (event.getClickCount() == 2 && event.getButton()== MouseButton.PRIMARY) {
            change(null);
        }
    }

    private Optional<ButtonType> tip(Student6699 student, String str){
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle(str + "提示");
        alert.setHeaderText("是否 " + str + "学号：" + student.getSno()
                + "，姓名：" + student.getSname() + " 的同学？");
        alert.getButtonTypes().setAll(
                ButtonType.YES,
                ButtonType.NO,
                ButtonType.CANCEL
        );
        return alert.showAndWait();
    }
}
